package cn.com.bluemoon.daps.common.domain;

import com.alibaba.fastjson.annotation.JSONField;

import java.io.Serializable;
import java.util.Optional;

import static org.apache.commons.lang3.builder.ToStringBuilder.reflectionToString;
import static org.apache.commons.lang3.builder.ToStringStyle.MULTI_LINE_STYLE;

/**
 * @author Jarod Kong
 * @date 2020-10-17 20:04
 **/
public class UserInfoHolder implements Serializable {
    private static final ThreadLocal<UserInfo> USER_THREAD_LOCAL = ThreadLocal.withInitial(() -> null);
    private static final long serialVersionUID = 1L;

    protected static final UserInfo GUEST = new UserInfo("未知用户", "未知用户", "");

    public static void setGuestUserInfo() {
        USER_THREAD_LOCAL.set(GUEST);
    }

    public static Optional<UserInfo> getUserInfo() {
        return Optional.ofNullable(USER_THREAD_LOCAL.get());
    }

    public static void setUserInfo(UserInfo userInfo) {
        USER_THREAD_LOCAL.set(userInfo);
    }

    public static UserInfo getUserInfoOrDefault(UserInfo userInfo) {
        return getUserInfo().orElse(userInfo);
    }

    public static UserInfo getUserInfoOrEmpty() {
        return getUserInfo().orElse(GUEST);
    }

    public static String getAccount(){
        return getUserInfoOrEmpty().getAccount();
    }

    public static String getUserName() {
        return getUserInfoOrEmpty().getUserName();
    }

    public static void clearUserInfo() {
        USER_THREAD_LOCAL.remove();
    }

    public static class UserInfo implements Serializable {
        private static final long serialVersionUID = 1L;
        @JSONField(name = "USER_NAME")
        private String userName;
        @JSONField(name = "EMAIL")
        private String email;
        @JSONField(name = "ID")
        private String account;
        @JSONField(name = "MOBILE")
        private String mobile;
        private String token;

        public UserInfo() {
        }

        public UserInfo(String userName, String account, String mobile) {
            this.userName = userName;
            this.account = account;
            this.mobile = mobile;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public String getAccount() {
            return account;
        }

        public void setAccount(String account) {
            this.account = account;
        }

        public String getMobile() {
            return mobile;
        }

        public void setMobile(String mobile) {
            this.mobile = mobile;
        }

        public String getToken() {
            return token;
        }

        public void setToken(String token) {
            this.token = token;
        }

        @Override
        public String toString() {
            return reflectionToString(this,  MULTI_LINE_STYLE);
        }
    }
}
